$(function () {
    // 授权角色关闭dialog
    $('#closeId2').click(function () {
        $("#dialogId2").dialog("close");
    });

    //编辑菜单
    $("#saveId2").click(function () {
        // method
        saveRoleMenu();
    });

});

/**
 * 用户授权角色-first
 */
function authMenu() {
    // open dialog dg
    openMenuDialog();

}

/**
 * open role-menu dialog
 */
function openMenuDialog() {
    var selRow = $("#dg").datagrid("getSelections");// 返回选中多行
    if (selRow.length == 0) {
        $.messager.alert("提示", "请选择一行数据！", "warning");
        return;
    }
    var roleId = selRow[0].id;

    // set role val
    $('#roleId').val(roleId);
    // getRoleMenu
    getRoleMenu(roleId);
}

/**
 * get all role-menu
 */
function getRoleMenu(roleId) {
    var menus = [];
    $.ajaxSync($WEB_ROOT_PATH + '/menu/getAllMenu', 'post', null, function (res) {
        if (res.code === 0) {
            menus = res.data;
        } else {
            $.messager.alert("提示", "菜单异常", "", function () {
                //window.location.href = $WEB_ROOT_PATH + "/";
            });
        }
    }, function (res) {
    }, 'json');

    $("#menuTreeId").tree({ //tree菜单全部加载
        data: menus,
        lines: false,
        animate: false,
        checkbox: true,
        cascadeCheck: false, // 不级联选择
        onlyLeafCheck: false, // 是否仅叶子节点复选框
        onLoadSuccess: function (node, data) {
            if (data) {
                $("#dialogId2").dialog({
                    closed: false,
                    closable: false,
                    /*left: 0,
                    top: 0,*/
                    title: '授权菜单',
                    iconCls: 'icon-save',
                    onOpen: function () {
                        // tree 回显
                        $.ajaxAsync($WEB_ROOT_PATH + '/menu/getMenuByRoleId', 'GET', {roleId: roleId}, function (res) {
                            if (res.code === 0) {
                                $(res.data).each(function (index, data) {
                                    var node = $('#menuTreeId').tree('find', data.menuId);
                                    $('#menuTreeId').tree('check', node.target);
                                });
                            } else {
                                resError(res);
                            }
                        }, function (res) {
                        }, 'json');
                    }
                });
            }
        },
        onCheck: function (node, checked) {
            // todo: 完美
            if (checked) {
                // parent node check
                var parentData = [];
                var parentIds = findParentIds(node, parentData);
                $(parentIds).each(function (index, id) {
                    var parentNode = $('#menuTreeId').tree('find', id);
                    if (!parentNode.checked) {
                        $('#menuTreeId').tree('check', parentNode.target);
                    }
                });
            }else{
                //children node uncheck
                var childrenIds = findChildrenIds(node);
                $(childrenIds).each(function (index, id) {
                    var childrenNode = $('#menuTreeId').tree('find', id);
                    if(childrenNode.checked){
                        $('#menuTreeId').tree('uncheck', childrenNode.target);
                    }
                });
            }
        },
        onClick: function (node) { //
        }
    });
}

/**
 * 递归向上查找父节点
 * @param node
 * @param parentIds
 * @returns {*}
 */
function findParentIds(node, parentIds) {
    if (null != node && '' != node && 'null' != node) {
        parentIds.push(node.id);
        var parentNode = $("#menuTreeId").tree("getParent", node.target);
        findParentIds(parentNode, parentIds);
    }
    return parentIds;
}

/**
 * 直接向下查找子节点
 * @param node
 * @param childrenIds
 * @returns {*}
 */
function findChildrenIds(node) {
    var childrenIds = [];
    if (null != node && '' != node && 'null' != node) {
        var childrenNode = $("#menuTreeId").tree("getChildren", node.target);
        $(childrenNode).each(function (index, node) {
            if(node.checked){
                childrenIds.push(node.id);
            }
        });
    }
    return childrenIds;
}

/**
 * 角色授权菜单
 */
function saveRoleMenu() {
    var roleId = $('#roleId').val();
    // todo:获取实心选中和check选中的数据
    var node = $("#menuTreeId").tree("getChecked", ['checked', 'indeterminate']);
    /*if (node.length == 0) {
        $.messager.alert('提示', '请至少选一个节点', "info");
        return;
    }*/
    var menuIds = [];
    for (var i = 0; i < node.length; i++) {
        if (null != node[i].id) {
            menuIds.push(node[i].id);
        }
    }
    if (menuIds.length == 0) {
        // delete flag
        menuIds.push("-1");
    }

    $.ajaxAsync($WEB_ROOT_PATH + '/role/authMenu', 'post', {menuIds: menuIds, roleId: roleId}, function (res) {
        if (res.code === 0) {
            $.messager.show({
                title: '提示',
                msg: res.data,
                timeout: 3000,
                showType: 'slide'
            });
        } else {
            resError(res);
        }
    }, function (res) {
    }, 'json');
}

/**
 * ajax req error
 * @param res
 */
function resError(res) {
    $.messager.alert('提示', res.data, "", function () {
    });
    $('.panel-tool-close').hide();
}